package com.archstoday.service;

import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Environment;
import android.widget.Toast;

import com.archstoday.common.SpfUtils;
import com.base.utils.MySharedPreferencesMgr;

import java.io.File;

public class DownLoadBroadcastReceiver  extends BroadcastReceiver {

    @SuppressLint("NewApi")
    public void onReceive(Context context, Intent intent) {
        long myDwonloadID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
        long refernece = MySharedPreferencesMgr.getLong(SpfUtils.DOWNLOAD_ID, 0);
        if (refernece == myDwonloadID) {
//            try {
//                String serviceString = Context.DOWNLOAD_SERVICE;
//                DownloadManager dManager = (DownloadManager) context.getSystemService(serviceString);
//                Intent install = new Intent(Intent.ACTION_VIEW);
//                Uri downloadFileUri = dManager.getUriForDownloadedFile(myDwonloadID);
//                install.setDataAndType(downloadFileUri, "application/vnd.android.package-archive");
//                install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                context.startActivity(install);
//            } catch (Exception e) {
//                Toast.makeText(context, "安装出错", Toast.LENGTH_SHORT).show();
//            }
            Toast.makeText(context, "下载成功", Toast.LENGTH_SHORT).show();
        }

    }
}